using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using AutoMapper;
using HuiLian.Common.Input;
using HuiLian.Common.Output;
using HuiLian.Model.Platform;
using HuiLian.Repository.Platform;
using HuiLian.Service.Platform.Pipe.Input;
//using HuiLian.Service.Platform.Pipe.Output;
using HuiLian.Common.Attributes;
using HuiLian.Common;
using System;

namespace HuiLian.Service.Platform.Pipe
{
    /// <summary>
    /// 管网资产服务
    /// </summary>
    public class PipeService : IPipeService
    {
        private readonly IMapper _mapper;
        private readonly IPipeRepository _pipeRepository;

        public PipeService(IMapper mapper, IPipeRepository pipeRepository)
        {
            _mapper = mapper;
            _pipeRepository = pipeRepository;
        }

        /// <summary>
        /// 获取管网资产信息
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<IResponseOutput> GetAsync(long id)
        {
            var result = await _pipeRepository.Select.Where(p => p.Id == id).FirstAsync();
            return ResponseOutput.Ok(result);
        }

        /// <summary>
        /// 查询管网资产信息
        /// </summary>
        /// <param name="input">分页信息</param>
        /// <returns></returns>
        public async Task<IResponseOutput> PageAsync(PageInput<PipeEntity> input)
        {
            try
            {
                //var key = input.Filter?.Label;

                var list = await _pipeRepository.Select
                .WhereDynamicFilter(input.DynamicFilter)
                //.WhereIf(key.NotNull(), a => a.Path.Contains(key) || a.Label.Contains(key))
                .Count(out var total)
                .OrderBy(true, c => c.Id)
                .Page(input.CurrentPage, input.PageSize)
                .ToListAsync();

                var data = new PageOutput<PipeEntity>()
                {
                    List = list,
                    Total = total
                };

                return ResponseOutput.Ok(data);
            }
            catch(Exception ex) { return ResponseOutput.NotOk(ex.Message + " " + ex.StackTrace); }
        }

        /// <summary>
        /// 添加管网资产信息
        /// </summary>
        /// <param name="input">管网资产信息</param>
        /// <returns></returns>
        public async Task<IResponseOutput> AddAsync(PipeAddInput input)
        {
            var entity = _mapper.Map<PipeEntity>(input);
            var id = (await _pipeRepository.InsertAsync(entity)).Id;

            return ResponseOutput.Result(id > 0);
        }

        /// <summary>
        /// 更新管网资产信息
        /// </summary>
        /// <param name="input">管网资产信息</param>
        /// <returns></returns>
        public async Task<IResponseOutput> UpdateAsync(PipeUpdateInput input)
        {
            if (!(input?.Id > 0))
            {
                return ResponseOutput.NotOk();
            }

            var entity = await _pipeRepository.GetAsync(input.Id);
            if (!(entity?.Id > 0))
            {
                return ResponseOutput.NotOk("数据不存在！");
            }

            _mapper.Map(input, entity);
            await _pipeRepository.UpdateAsync(entity);
            return ResponseOutput.Ok();
        }

        /// <summary>
        /// 删除管网资产信息
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<IResponseOutput> DeleteAsync(long id)
        {
            var result = false;
            if (id > 0)
            {
                result = (await _pipeRepository.DeleteAsync(m => m.Id == id)) > 0;
            }

            return ResponseOutput.Result(result);
        }

        /// <summary>
        /// 软删除管网资产信息
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public async Task<IResponseOutput> SoftDeleteAsync(long id)
        {
            var result = await _pipeRepository.SoftDeleteAsync(id);
            return ResponseOutput.Result(result);
        }

        /// <summary>
        /// 批量软删除管网资产信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<IResponseOutput> BatchSoftDeleteAsync(long[] ids)
        {
            var result = await _pipeRepository.SoftDeleteAsync(ids);

            return ResponseOutput.Result(result);
        }

        /// <summary>
        /// 添加列表
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public async Task<IResponseOutput> AddListAsync(List<PipeEntity> list)
        {
            var entitys = await _pipeRepository.Select.ToListAsync();
            List<PipeEntity> addList = new List<PipeEntity>();
            foreach (var item in list)
            {
                var entity = entitys.Where(p => p.管段编号 == item.管段编号).FirstOrDefault();
                if (entity == null) addList.Add(item);
            }

            if (addList.Count > 0)
            {
                var count = (await _pipeRepository.InsertAsync(addList)).Count;
                return ResponseOutput.Result(count > 0);
            }
            return ResponseOutput.Result(true);
        }

    }
}
